//
//  ClassIcJokeViewController.m
//  HaHa
//
//  Created by 元虎的mac on 15/9/18.
//  Copyright (c) 2015年 吉亚平. All rights reserved.
//

#import "ClassIcJokeViewController.h"
#import "ClassJokeTableViewCell.h"
#import "LORequestManger.h"
#import "ClassJokeModel.h"
#import "todayJokeModel.h"
#import "WeekJokeModel.h"
#import "detailsClassicJokeViewController.h"
@interface ClassIcJokeViewController ()
@property (nonatomic, strong)UIActivityIndicatorView *activity;

//标签控制器
@property (nonatomic)UISegmentedControl *Segmengt;

@property (nonatomic)UITableView *tableView;

@property (nonatomic)UITableView *classTableView;

@property (nonatomic)UITableView *todayTableView;

@property (nonatomic)UITableView *weekTableView;

@property (nonatomic)UIScrollView *scrollView;
//声明
@property (nonatomic, strong)NSMutableArray *ClassJokeArr;

@property (nonatomic, strong)NSMutableArray *todayJokeArr;

@property (nonatomic, strong)NSMutableArray *weekJokeArr;


//声明一个类
@property (nonatomic, strong)ClassJokeTableViewCell *cell;
@end

@implementation ClassIcJokeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self addSubView];
    [self addScroll];
    [self addTableView];
    [self reloadDataForClassJoke];
     __block ClassIcJokeViewController *root = self;
    [self.classTableView addHeaderWithCallback:^{
       //[root reloadDataForClassJoke];
        [root.classTableView headerEndRefreshing];
        
    }];
    [self.classTableView addFooterWithCallback:^{
        //[root reloadDataForClassJoke];
        [root.classTableView footerEndRefreshing];
        
    }];

   
    
    [self.todayTableView addHeaderWithCallback:^{
        //[root reloadDataForClassJoke];
        [root.todayTableView headerEndRefreshing];
    }];
    [self.todayTableView addFooterWithCallback:^{
        //[root reloadDataForClassJoke];
        [root.todayTableView footerEndRefreshing];
    }];
    
    
    [self.weekTableView addHeaderWithCallback:^{
        //[root reloadDataForClassJoke];
        [root.weekTableView headerEndRefreshing];
    }];
    [self.weekTableView addFooterWithCallback:^{
        //[root reloadDataForClassJoke];
        [root.weekTableView footerEndRefreshing];
    }];
    
    // Do any additional setup after loading the view.
    
    [self AddAcTivityView];
}

#pragma mark -------懒加载---------
-(NSMutableArray *)weekJokeArr
{
    if (!_weekJokeArr) {
        _weekJokeArr = [[NSMutableArray alloc] init];
    }
    return _weekJokeArr;
}
-(NSMutableArray *)todayJokeArr
{
    if (!_todayJokeArr) {
        _todayJokeArr = [[NSMutableArray alloc] init];
    }
    return _todayJokeArr;
}
-(NSMutableArray *)ClassJokeArr
{
    if (!_ClassJokeArr) {
        _ClassJokeArr = [[NSMutableArray alloc] init];
    }
    return _ClassJokeArr;
}
-(void)reloadDataForClassJoke
{
    NSInteger index = self.Segmengt.selectedSegmentIndex;
    //NSLog(@"%ld",index);
    
    
    if (index == 0) {
        UITableView *temp = (UITableView *)[self.view viewWithTag:1000];
        [LORequestManger GET:KTEXTconnotationKUrl success:^(id response) {
            NSDictionary *dic = (NSDictionary *)response;
            NSDictionary *DataDIC = [dic objectForKey:@"data"];
            NSArray *DataArr = [DataDIC objectForKey:@"data"];
            //NSLog(@"%@",DataArr);
            //NSLog(@"%@",[DataArr lastObject]);
            for (NSDictionary *commentsDic in DataArr) {
                ClassJokeModel *classmodel = [ClassJokeModel shareValueWithDic:commentsDic];
                
                [self.ClassJokeArr addObject:classmodel];
            }
            [temp reloadData];
            //[_activity stopAnimating];
            [self animationD];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];
    }else if (index == 1){
        UITableView *temp =(UITableView *) [self.view viewWithTag:1001];
        [LORequestManger GET:KTEXTDayUrl success:^(id response) {
            NSDictionary *Dic = (NSDictionary *)response;
            NSArray *todayArr = [Dic objectForKey:@"data"];
            for (NSDictionary *commentsDic in todayArr) {
                todayJokeModel *model = [todayJokeModel setValueWithDic:commentsDic];
                [self.todayJokeArr addObject:model];
            }
            [temp reloadData];
            //[_activity stopAnimating];
            [self animationD];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];
    }else{
        UITableView *temp = (UITableView *)[self.view viewWithTag:1002];
        [LORequestManger GET:KTEXTWeekUrl success:^(id response) {
            NSDictionary *dic = (NSDictionary *)response;
            NSArray *weekArr = [dic objectForKey:@"data"];
            for (NSDictionary *contentDic in weekArr) {
                WeekJokeModel *model = [WeekJokeModel shareValueWithdic:contentDic];
                [self.weekJokeArr addObject:model];
            }
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];
        [LORequestManger GET:KTEXTMonthUrl success:^(id response) {
            NSDictionary *montDic = (NSDictionary *)response;
            NSArray *monthArr = [montDic objectForKey:@"data"];
            for (NSDictionary *monthdic in monthArr) {
               WeekJokeModel *monthModel = [WeekJokeModel shareValueWithdic:monthdic];
                [self.weekJokeArr addObject:monthModel];
            }
            [temp reloadData];
            //[_activity stopAnimating];
            [self animationD];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];
    }
}

#pragma mark ------------添加风火轮----------------
- (void)AddAcTivityView{
    self.activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.activity.frame = CGRectMake(kScreenWdth/2-40, kScreenHeight/2-40, 80, 80);
    self.activity.layer.cornerRadius = 5;
    self.activity.alpha = 0.9;
    self.activity.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.activity.layer.borderWidth = 1;
    [self.activity startAnimating];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 80, 20)];
    label.font = [UIFont boldSystemFontOfSize:11];
    label.text = @"正在加载..";
    label.textAlignment = NSTextAlignmentCenter;
    _activity.backgroundColor = [UIColor grayColor];
    [_activity addSubview:label];
    [self.view addSubview:_activity];
}
- (void)animationD{
    CATransition *transition = [CATransition animation];
    //指定动画类型
    transition.type = @"rippleEffect";//效果类型
    transition.subtype = kCATransitionFromBottom;//方向类型 决定动画的开始方向
    transition.duration = 0.4; //单次动画时长
    [self.view.superview.layer addAnimation:transition forKey:@"但是"];
    [self.tableView setContentOffset:CGPointMake(0, -64) animated:YES];
}


//添加子视图
-(void)addSubView
{
    [self addSegment];
}

#pragma mark --------------下拉刷新--------------
//-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
//{
//    if (scrollView.contentOffset.y < -150) {
//        //[_activity startAnimating];
//        [self reloadDataForClassJoke];
//    }

//}
-(void)addScroll
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWdth, kScreenHeight - 113)];
    self.scrollView.contentSize = CGSizeMake(kScreenWdth * 3, 0);
    self.scrollView.backgroundColor = [UIColor yellowColor];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    [self.view addSubview:self.scrollView];
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.scrollView == scrollView) {
        self.Segmengt.selectedSegmentIndex = self.scrollView.contentOffset.x / kScreenWdth;
        [self reloadDataForClassJoke];
    }
    
}
//添加标签视图
-(void)addSegment{
    //导航控制器的标题视图
    UIView *TitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 4, kScreenWdth - 20, 30)];
    self.Segmengt = [[UISegmentedControl alloc] initWithItems:@[@"人气推荐",@"今日最热",@"本月最热"]];
    self.Segmengt.frame = TitleView.frame;
    self.Segmengt.selectedSegmentIndex = 0;
    self.Segmengt.tintColor = [UIColor purpleColor];
    [self.Segmengt addTarget:self action:@selector(SegmengtClick) forControlEvents:(UIControlEventValueChanged)];
    [TitleView addSubview:self.Segmengt];
    self.navigationItem.titleView = TitleView;
}
-(void)SegmengtClick
{
    [self.scrollView setContentOffset:CGPointMake(self.Segmengt.selectedSegmentIndex * kScreenWdth, 0) animated:YES];
    
    //[_activity startAnimating];
    [self reloadDataForClassJoke];
}

-(void)addTableView
{
    
       self.classTableView = [[UITableView alloc] initWithFrame:CGRectMake(10 , 0, kScreenWdth - 20, self.scrollView.frame.size.height)];
      self.classTableView.backgroundColor = [UIColor grayColor];
       self.classTableView.delegate = self;
        self.classTableView.dataSource = self;
    self.classTableView.tag = 1000;
        [self.scrollView addSubview:self.classTableView];
        //[self AddAcTivityView];
    self.todayTableView = [[UITableView alloc] initWithFrame:CGRectMake(10 + kScreenWdth, 0, kScreenWdth - 20, self.scrollView.frame.size.height)];
    self.todayTableView.backgroundColor = [UIColor grayColor];
    self.todayTableView.delegate = self;
    self.todayTableView.dataSource = self;
    self.todayTableView.tag = 1001;
    [self.scrollView addSubview:self.todayTableView];
    self.weekTableView = [[UITableView alloc] initWithFrame:CGRectMake(10 + kScreenWdth * 2, 0, kScreenWdth - 20, self.scrollView.frame.size.height)];
    self.weekTableView.backgroundColor = [UIColor grayColor];
    self.weekTableView.delegate = self;
    self.weekTableView.dataSource = self;
    self.weekTableView.tag = 1002;
    [self.scrollView addSubview:self.weekTableView];
    
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.Segmengt.selectedSegmentIndex == 0) {
        
         return self.ClassJokeArr.count;
    }else if (self.Segmengt.selectedSegmentIndex == 1){
        
        return self.todayJokeArr.count;
    }else {
        
        return self.weekJokeArr.count;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
          static NSString *identifier =@"cell";
    self.cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!self.cell) {
       self.cell = [[ClassJokeTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    if (tableView.tag == 1000) {
        ClassJokeModel *classJokeModel = [[ClassJokeModel alloc] init];
        classJokeModel = [self.ClassJokeArr objectAtIndex:indexPath.row];
        [self.cell setvalueWithModel:classJokeModel];
        [_activity stopAnimating];
        return self.cell;

    }else if(tableView.tag == 1001){
        todayJokeModel *todayModel = [[todayJokeModel alloc] init];
        todayModel = [self.todayJokeArr objectAtIndex:indexPath.row];
        [self.cell SetValueWithModel:todayModel];
        [_activity stopAnimating];
        return self.cell;
    }else
    {
        WeekJokeModel *weekModel = [[WeekJokeModel alloc] init];
        weekModel = [self.weekJokeArr objectAtIndex:indexPath.row];
        [self.cell setValueWithmodel:weekModel];
        [_activity stopAnimating];
        return self.cell;
    }
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     detailsClassicJokeViewController *detailsClassicJoke = [[detailsClassicJokeViewController alloc] init];
    if (self.Segmengt.selectedSegmentIndex == 0) {
       ClassJokeModel *model = self.ClassJokeArr[indexPath.row];
        detailsClassicJoke.classModel = model;
        detailsClassicJoke.groupID = model.group_id;
    }else if (self.Segmengt.selectedSegmentIndex == 1){
        todayJokeModel *model = self.todayJokeArr[indexPath.row];
        detailsClassicJoke.todayModel = model;
        detailsClassicJoke.groupID =model.group_id;
    }else{
        WeekJokeModel *model = self.weekJokeArr[indexPath.row];
        detailsClassicJoke.weekModel = model;
        detailsClassicJoke.groupID = model.group_id;
    }
    [self.navigationController pushViewController:detailsClassicJoke animated:YES];
  
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.cell.commentLabel.frame.origin.y + self.cell.commentLabel.frame.size.height + 5 + 40;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
